package com.qf.realm;

import com.qf.bean.User;
import com.qf.mapper.UserMapper1;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * projectName: 2103--RBAC
 *
 * @author: MaTianWei
 * time: 2021/7/10 9:59
 * description:自定义realm
 */

public class AuthoringRealm extends AuthorizingRealm {
    //doGetAuthorizationInfo获取授权信息
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //1·获取用户的信息 账号 id
        User user = (User) principals.getPrimaryPrincipal();
        //2·查询用户对应的角色和权限
        InputStream stream = null;
        try {
            stream = Resources.getResourceAsStream("mybatis_config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(stream);

        SqlSession sqlSession = sessionFactory.openSession();
        UserMapper1 mapper = sqlSession.getMapper(UserMapper1.class);

        List<String> roles = mapper.queryRoles(user.getuId());
        List<String> permissions = mapper.queryPerMission(user.getuId());

        //3·角色和权限转到AuthorizationInfo
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();

        authorizationInfo.addRoles(roles);
        authorizationInfo.addStringPermissions(permissions);

        return authorizationInfo;
    }
    //doGetAuthenticationInfo获取认证信息
    //当Subject调用Login方法的时候会触发，并且将封装用户输入账号密码token传入此方法
    //根据token的账号，查询正确的密码给SecurityManager

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //1·获取token的账号
       String account = (String) token.getPrincipal();//输入的账号部分
        //2·拿账号去数据库查询
        InputStream stream = null;
        try {
            stream = Resources.getResourceAsStream("mybatis_config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(stream);

        SqlSession sqlSession = sessionFactory.openSession();

        UserMapper1 mapper = sqlSession.getMapper(UserMapper1.class);

        User user = mapper.queryByAccount(account);

        //3·如果未查询到返回null
        if (user == null) {
            return null;
        }
        //4·如果根据账号查询到对应的用户
        SimpleAuthenticationInfo
                info = new SimpleAuthenticationInfo
                (user,user.getuPassword(), ByteSource.Util.bytes(user.getuSlat()),"myrealm");
        //5·只需要返回密码

        return info;
    }
}
